﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using dateDealNamespace;

namespace GDIPlusDemo
{
    public class Display
    {
        dataDeal mydataDeal = new dataDeal();

        #region 电子屏引用
        [DllImport("SOURDLL.DLL", CharSet = CharSet.Ansi, EntryPoint = "disp", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        public extern static byte disp(int comm, int btl, int delayus, int number, int scantype, int scanmode, int x, int y, int page, int pagecount, int mode, int speed, int delaytime, int colorswap, int rgswap, int refresh, StringBuilder bitmapfilename);
        public int _screenShowTime = 3000;                                      //显示屏刷新时间
        #endregion

        #region 显示屏处理
        /// <summary>
        /// 显示屏显示
        /// </summary>

        private int _cmbPortIndex = -1;
        private string _screenTemp;     //显示屏显示温度
        private string _screenHumi;     //显示屏显示湿度
        private string _screenIllu;     //显示屏显示光照
        private string modeName;        //显示屏显示模式名称
        private string modeArea;        //显示屏显示模式区域号

        /// <summary>
        /// 刷新显示屏数据
        /// </summary>
        public void electScreenShow()
        {
            if (!File.Exists("SOURDLL.DLL"))
            {
                _cmbPortIndex = -1;
                MessageBox.Show(@"未能找到 SOURDLL.DLL 引用文件，显示屏不可用。", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            var sb = new StringBuilder("screen.bmp");    //图片的路径
            var intPortName = 3;                         //串口名
            try
            {
                var intResult = disp(intPortName, 9600, 50, 1, 0, 16, 128, 64, 1, 1, 0, 10, 5, 0, 0, 1, sb);                 //调用显示屏函数
                switch (intResult)
                {
                    case 1:
                        _cmbPortIndex = -1;
                        MessageBox.Show(@"显示失败，请检测本目录是否可写", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        break;
                    case 0:
                        _cmbPortIndex = -1;
                         MessageBox.Show(@"显示失败，请检测显示屏是否被占用", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        break;
                }
            }
            catch (Exception)
            {
                _cmbPortIndex = -1;
                 MessageBox.Show(@" SOURDLL.DLL 引用文件失效，显示屏不可用。", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
        /// <summary>
        /// 更新缓冲图片
        /// 显示屏显示数据查询
        /// </summary>
        public void updateImage()
        {
            string[] modeIdArr = mydataDeal.modeId();
            string[] modeAreaArr;
            string[] displayData;
            string modeIdTemp;
            string area;
            for (int i = 0; i < modeIdArr.Length; i++)
            {
                modeIdTemp = modeIdArr[i];
                if (modeIdTemp != "0")
                {
                    modeAreaArr = mydataDeal.modeArea(modeIdTemp);
                    for (int j = 0; j < modeAreaArr.Length; j++)
                    {
                        area = modeAreaArr[j];

                        if (area != null)
                        {
                            displayData = mydataDeal.modeAreaDataDisplay(modeIdTemp, area);
                            modeName = displayData[0];
                            modeArea = displayData[1];
                            _screenTemp = displayData[2];
                            _screenIllu = displayData[3];
                            _screenHumi = displayData[4];

                            var bp = new Bitmap(128, 64, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
                            var gp = Graphics.FromImage(bp);
                            gp.FillRectangle(Brushes.White, RectangleF.FromLTRB(0, 0, 128, 64));
                            bp.SetPixel(0, 0, Color.White);
                            var ft = new Font("宋体", 9, FontStyle.Regular, GraphicsUnit.Point);
                            gp.DrawString("场景：", ft, Brushes.Black, new PointF(5f, 1));
                            gp.DrawString(modeName, ft, Brushes.Black, new PointF(50f, 1));
                            gp.DrawString("区域：", ft, Brushes.Black, new PointF(5f, 17));
                            gp.DrawString(modeArea, ft, Brushes.Black, new PointF(38f, 17));
                            gp.DrawString(" " + DateTime.Now.ToShortDateString(), ft, Brushes.Black, new PointF(45f, 17));
                            gp.DrawString("温度  光照 土壤湿度", ft, Brushes.Black, new PointF(5f, 34));
                            gp.DrawString(_screenTemp + "℃", ft, Brushes.Black, new PointF(5f, 50));
                            gp.DrawString(_screenIllu + "L", ft, Brushes.Black, new PointF(41f, 50));
                            gp.DrawString(_screenHumi + "%", ft, Brushes.Black, new PointF(86f, 50));
                            try
                            {
                                bp.Save("screen.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                            electScreenShow();                          //显示屏上显示数据
                            Thread.Sleep(6000);

                            //MessageBox.Show("modeName:" + modeName + "\nmodeArea:" + modeArea + "\n_screenTemp:" + _screenTemp + "\n_screenIllu:" + _screenIllu + "\n_screenHumi:" + _screenHumi);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
